package com.example.mynote.fragment;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatImageButton;

import com.example.mynote.R;
import com.example.mynote.activity.AddActivity;
import com.example.mynote.activity.EditNoteActivity;
import com.example.mynote.base.BaseFragment;
import com.example.mynote.entity.Collect;
import com.example.mynote.entity.Note;
import com.example.mynote.entity.StickyNote;

import org.litepal.LitePal;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class NoteFragment extends BaseFragment {

    private EditText noteSearch;
    private ListView noteList;
    private List<Note> data = new ArrayList<>();
    private AppCompatImageButton noteAdd;
    private BaseAdapter adapter;

    @Override
    protected int Layout() {
        return R.layout.fragment_note;
    }

    @Override
    protected void initView(View v) {

        noteSearch = (EditText) v.findViewById(R.id.note_search);
        noteList = (ListView) v.findViewById(R.id.note_list);
        noteAdd = (AppCompatImageButton) v.findViewById(R.id.note_add);
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void initData() {
        List<Note> all = LitePal.findAll(Note.class);
        data = all;

        noteSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!s.toString().equals("")) {
                    List<Note> list = new ArrayList<>();
                    for (int i = 0; i < all.size(); i++) {
                        if (all.get(i).getTitle().contains(s) || all.get(i).getContent().contains(s)) {
                            list.add(all.get(i));
                        }
                    }
                    data = list;
                } else {
                    data = all;
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        adapter = new BaseAdapter() {
            private TextView listContent;
            private TextView listHour;
            private TextView listWeek;
            private TextView listDay;

            private void initView(View view) {
                listDay = (TextView) view.findViewById(R.id.list_day);
                listWeek = (TextView) view.findViewById(R.id.list_week);
                listHour = (TextView) view.findViewById(R.id.list_hour);
                listContent = (TextView) view.findViewById(R.id.list_content);
            }

            @Override
            public int getCount() {
                return data.size();
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view;
                if (convertView == null) {
                    view = LayoutInflater.from(getActivity()).inflate(R.layout.note_list_item, null);
                } else {
                    view = convertView;
                }

                initView(view);
                SimpleDateFormat day = new SimpleDateFormat("dd", Locale.CHINA);
                SimpleDateFormat hour = new SimpleDateFormat("HH:mm", Locale.CHINA);
                SimpleDateFormat week = new SimpleDateFormat("E", Locale.CHINA);
                listDay.setText(day.format(new Date(data.get(position).getTime())));
                listWeek.setText(week.format(new Date(data.get(position).getTime())));
                listHour.setText(hour.format(new Date(data.get(position).getTime())));
                listContent.setText(data.get(position).getContent());
                return view;
            }
        };
        noteList.setAdapter(adapter);

        noteAdd.setOnClickListener(v -> {
            intentTo(AddActivity.class);
        });

        noteList.setOnItemLongClickListener((parent, view, position, id) -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("收藏")
                    .setMessage("你确定要收藏这条日记吗？")
                    .setPositiveButton("确定", (dialog, which) -> {
                        new Collect(data.get(position).getTitle(), data.get(position).getContent(), data.get(position).getTime()).save();
                        dialog.cancel();
                    }).setNegativeButton("取消", (dialog, which) -> dialog.cancel())
                    .show();
            return true;
        });

        noteList.setOnItemClickListener((parent, view, position, id) -> {
            startActivity(new Intent(getActivity(),EditNoteActivity.class).putExtra("id", data.get(position).getId()));
        });
    }
}